﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace NDF.XnaLibrary.Models
{
	public class Equipment : GameModel, IItem
	{
		#region Fields

		private int mId;
		private EquipmentType mType;
		private EquipmentSlot mSlot;

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the equipment class with the given settings.
		/// </summary>
		/// <param name="Id">The ID of the equipment.</param>
		/// <param name="slot">The slot of the equipment.</param>
		/// <param name="name">The name of the equipment to be displayed.</param>
		/// <param name="model">The equipment`s model.</param>
		public Equipment(int id, EquipmentType type, string name, Model model) :
			base(model, name)
		{
			this.mId = id;
			this.mType = type;	
		}

		#endregion

		#region Properties

		/// <summary>
		/// Gets the ID of the equipment.
		/// </summary>
		public int Id
		{
			get { return mId; }
		}

		/// <summary>
		/// Gets the slot of the equipment.
		/// </summary>
		public EquipmentType Type
		{
			get { return mType; }
		}

		/// <summary>
		/// Gets or sets the slot at which the equipment is currently equipped in.
		/// </summary>
		public EquipmentSlot Slot
		{
			get { return mSlot; }
			set { mSlot = value; }
		}

		/// <summary>
		/// Gets or sets the stats of the equipment.
		/// </summary>
		public CombatStatistics Stats { get; set;}

		/// <summary>
		/// Gets or sets the icon of the equipment.
		/// </summary>
		public Texture Icon { get; set; }

		/// <summary>
		/// Gets or sets the monetary value of the item.
		/// </summary>
		public float Value { get; set; }

		/// <summary>
		/// Gets or sets a boolean showing whether the item is textured or not
		/// </summary>
		public bool IsTextured { get; set; }

		#endregion

		#region Methods

		/// <summary>
		/// Saves the changes you`ve made to the equipment`s effect. 
		/// <para>Call this method each time you modify the equipment`s effect.</para>
		/// <para>Load the equipment model prior to using this method.</para>
		/// <exception cref="InvalidOperationException"></exception>
		/// </summary>
		public override void SaveEffectChanges()
		{
			base.SaveEffectChanges(Effect);
		}

		/// <summary>
		/// Draws the item.
		/// </summary>
		/// <param name="camera">The camera to draw according to.</param>
		/// <param name="worldMatrix">The world matrix to be used.</param>
		public void Draw(Matrix worldMatrix, bool useShadowMap)
		{
			if (!mIsLoaded)
			{
				throw new InvalidOperationException("Cant draw the equipment prior to loading it. Load the model first.");
			}

			foreach (ModelMesh mesh in this.mModel.Meshes)
			{
				foreach (Effect effect in mesh.Effects)
				{
					string technique = useShadowMap ? "ShadowMap" : (IsTextured ? "TexturedShaded" : "ColoredShaded");
					effect.CurrentTechnique = effect.Techniques[technique];
					effect.Parameters["World"].SetValue(worldMatrix);
					effect.Parameters["View"].SetValue(Camera.Active.ViewMatrix);
					effect.Parameters["Projection"].SetValue(Camera.Active.ProjectionMatrix);
				}
				mesh.Draw();
			}
		}

		#endregion
	}
}
